<div id="container">
	<?php  if (!$this->allow()) : ?>
		<div class="grid_12 t_a_bottom">
			<?php echo $this->translator->translator('Member_Access_deny_desc'); ?>
		</div>
	<?php else : ?>
	
		<?php 
				  
			$version = Zend_Registry::get('version'); 
			
			$text_search_field = '';
			foreach($this->table_fields_arr as $key => $value)
			{
				$text_search_field .= '<option value="'.$value.'">'.$value.'</option>';
			}
			$text_search_field	.= '</select>';
			
			$text_search_type  = '';
			foreach ($this->text_search_logic as $key => $value)
			{
				$text_search_type .= '<option value="'.$key.'">'.$value.' ('.$key.')</option>';
			}
			$text_search_type .= '</select>';
			
			foreach ($this->numeric_search_logic as $key => $value)
			{
				$numeric_search_type .= '<option value="'.$key.'">'.$value.' ('.$key.')</option>';
			}
			$numeric_search_type .= '</select>';
			
			foreach ($this->date_search_logic as $key => $value)
			{
				$date_search_type .= '<option value="'.$key.'">'.$value.' ('.$key.')</option>';
			}
			$date_search_type .= '</select>';
		?>
		
		<script type="text/javascript">
			function openMsgDialog(msg)
			 {
				$('#dialog_msg').html(msg).dialog({
					autoOpen: true,
					modal: true,
					show: 'fade',
					resizable: 'false',				
					buttons: { "<?php echo html_entity_decode($this->translator->translator('common_ok'), ENT_QUOTES, 'UTF-8'); ?>": function() { $(this).dialog('option', 'hide', 'explode').dialog("close"); } },
					open: function(event, ui)
						 {
							setTimeout(function() 
							{									
								if($('#' + event.target.id).dialog('isOpen'))
								{
									$('#' + event.target.id).dialog('option', 'hide', 'explode').dialog('close');										
								}
							}, 10000);
						  }
				});
			 }
			
			$('a.reset_btn').click(function() {			
				 resetFields();	
				 removeError();	
			});
			
			function resetFields()
			{
				$( "form" )[ 0 ].reset();
			}
			
			function removeError()
			{
				$('#actionMessage').html('');
				
				var filename = '';
				var filevalue = '';		
				$.each($('form').serializeArray(), function(i, field) {	
						if(field.name.match(/\[\]$/))
						{
							var filearr = field.name.split('[]');
							filename = filearr[0];					
						}
						else
						{
							filename 	= 	field.name;
						} 			
						$('#'+filename).removeClass('ui-state-error');
						$('#'+filename+'_err').html('');									
				});				
			}
			
			function addError(key,errMsg)
			{
				var err_id = key+'_err';
				var field_name = key;
				$('#'+field_name).addClass('ui-state-error');
				$('#'+err_id).append(errMsg);
			}
			
			function openLoaderDialog()
			{
				$('#dialog_loader').dialog({
							autoOpen: true,
							modal: true,
							show: 'fade',
							resizable: 'false',				
							open: function(event, ui)
								 {
									$("#progressbar").progressbar({
										value:100
						
									});							
								  }
						});
			}
			
			function closeLoaderDialog()
			{
				$('#dialog_loader').dialog('option','hide','explode').dialog('close');
			}
			
			function getFormData()
			{
				var fieldObj = {};
				var filename = '';
				var filevalue = '';
				$.each($('form').serializeArray(), function(i, field) {	
					if(field.name.match(/\[\]$/))
					{
						var filearr = field.name.split('[]');
						filename = filearr[0];
						filevalue = $('#'+filename).val();
						if($('input[name="'+filename+'[]"]').is(':checkbox'))
						{
							var val_arr = [];
							$('input[name="'+filename+'[]"]:checkbox:checked').each(function(i){
							 val_arr[i] = $(this).val();
							});
							filevalue = (val_arr[0] != null) ? val_arr.join(',') : null;					
						}
					}
					else
					{
						filename 	= 	field.name;
						filevalue 	= 	field.value;
					}			
					fieldObj[filename] = filevalue;			
				});
				return fieldObj;
			}
			
			function removeRows()
			{
				$('a.remove_search_btn_link').unbind('click');
				$('a.remove_search_btn_link').click(function() {			
		 			var self = this;
					var rel = $(self).attr('rel');
					var count = $('#'+rel+'_cnt').val();
					count = parseInt(count);					
					$(self).parents('div:first').parents('div:first').parents('div:first').remove();	
				});	
			}
				
			$(document).ready(function() {
				
				floatingbar('#floatingbar',"#button_top_bar",null);
				
				removeRows();
			
				$('a.add_new_search_btn_link').click(function() {			
		 			var self = this;
					var rel = $(self).attr('rel');
					switch(rel)
					{
						case 'text':
							var search_type = '<?php echo $text_search_type; ?>';
							break;
						case 'numeric':
							var search_type = '<?php echo $numeric_search_type; ?>';
							break;
						case 'date':
							var search_type = '<?php echo $date_search_type; ?>';
							break;
					}
					var count = $('#'+rel+'_cnt').val();
					count = parseInt(count);
					var putData ='<div><div class="search_col_1">';
						putData += '<select name="'+rel+'_search_field'+count+'" id="'+rel+'_search_field'+count+'" class="dynamic_search_field"><?php echo $text_search_field; ?>';
						putData += '</div>';
					
						putData += '<div class="search_col_2">';
					    putData += '<select name="'+rel+'_search_type'+count+'" id="'+rel+'_search_type'+count+'" class="dynamic_search_field">'+search_type;
						putData += '</div>';
					
						putData += '<div class="search_col_3"><?php echo $this->translator->translator("settings_search_respective_search_title"); ?>&nbsp;&nbsp;';
						putData += '<input type="radio" name="'+rel+'_search_option'+count+'" id="'+rel+'_search_option'+count+'" value="0" checked="checked" />&nbsp;'; 
						putData += "<?php echo $this->translator->translator('settings_search_logic_optional'); ?>";
						putData += '&nbsp;&nbsp;<input type="radio" name="'+rel+'_search_option'+count+'" id="'+rel+'_search_option'+count+'" value="1" />&nbsp;'; 
						putData += "<?php echo $this->translator->translator('settings_search_logic_mandatory'); ?>";
						putData += '</div>';
					
						putData += '<div class="search_col_4">';
						putData += '<div class="remove-btn ui-widget-content-1"><a href="javascript:void(0);" rel="'+rel+'" class="remove_search_btn_link"><span class="icon delete"><?php echo $this->translator->translator("settings_search_field_remove"); ?></span></a></div>'; 							
						putData += '</div></div>';
					$('#search_container_'+rel).append(putData);
					count++;
					$('#'+rel+'_cnt').val(count);
					removeRows();
				});
				
				$('a.save_search_btn').click(function() {
					var self = this;
					var formData = getFormData();
					//alert(formData.toSource());
					$.ajax({
							url: "<?php echo $this->url( array('module'=> 'Settings', 'controller' => 'search', 'action'     => 'update', 'group_id' => $this->escape($this->group_id), 'group_form_id_field' => $this->group_form_id_field, 'group_table' => $this->group_table, 'static_table' => $this->static_table ), 'adminrout',    true) ?>",
								type: 'POST',
								data: formData,
								beforeSend: function(){						
										removeError();
										openLoaderDialog();				
									 },
								success: function(response){
									//alert(response);					
									var json_arr = eval("("+response+")");					
									closeLoaderDialog();					
									if(json_arr.status == 'ok')
									{
										$('#actionMessage').html(succMsgDesign(json_arr.msg));																	
									}
									else if(json_arr.status == 'errV')
									{
										$('#actionMessage').html(errMsgDesign("<?php echo $this->translator->translator('common_err'); ?>"));
																
										for(var i= 0; i < json_arr.msg.length; i++)
										{
											addError(json_arr.msg[i].key,json_arr.msg[i].value);							
										}
									}
									else
									{
										$('#actionMessage').html(errMsgDesign(json_arr.msg));
									}
								},
								error: function(xhr, status, error){
									closeLoaderDialog();
									var msg = "Error! " + xhr.status + " " + error;
									$('#actionMessage').html(errMsgDesign(msg));
								}
						});
				});		
			});
		</script>        
        <div id="button_top_bar"></div>
        <div class="title-container">
            <div class="search-setting"><h1><?php echo $this->translator->translator('settings_search_page_name'); ?></h1></div>
            
            <div class="button-container">
                <span class="button  ui-widget-content-1"><a href="<?php echo $this->url( array('module' => $this->back_module_name,'controller' => $this->back_controller_name, 'action'     => $this->back_action_name ), 'adminrout',    true) ?>"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
                <span class="button  ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
                <span class="button  ui-widget-content-1"><a href="javascript: void(0);" class="save_search_btn"><span class="icon save"><?php echo $this->translator->translator('common_save'); ?></span></a></span>
            </div>
        </div>
        
        <div class="content-container">
            <div id="actionMessage"></div>
        	<form name="formSearchSettings" id="formSearchSettings" method="post">
				<div id="generalInfo">
                	<!-- Text Search Start -->
                    <fieldset class="fieldset">
                    	<legend class="legend dynamic_search_title"><?php echo $this->translator->translator('settings_search_text_search_title'); ?></legend>
						<div class="search_container" id="search_container_text">
								<?php 
									if($this->search_settings_data)
									{
										$cnt = 0;
										foreach($this->search_settings_data as $entry)
										{
											if(!empty($entry['text_search_field']))
											{
												echo '<div><div class="search_col_1">';
													echo '<select name="text_search_field'.$cnt.'" id="text_search_field'.$cnt.'"  class="dynamic_search_field">';
													echo '<option value="">'.$this->translator->translator('common_select').'</option>';
													foreach ($this->table_fields_arr as $key => $value)
													{
														$selected = ($value == $entry['text_search_field']) ? ' selected="selected" ' : '';
														echo '<option value="'.$value.'"'.$selected.'>'.$value.'</option>';
													}
													echo '</select>';
												echo '</div>';
												
												echo '<div class="search_col_2">';
													echo '<select name="text_search_type'.$cnt.'" id="text_search_type'.$cnt.'" class="dynamic_search_field">';
													foreach ($this->text_search_logic as $key => $value)
													{
														$selected = ($key == $entry['text_search_type']) ? ' selected="selected" ' : '';
														echo '<option value="'.$key.'"'.$selected.'>'.$value.' ('.$key.')</option>';
													}					
													echo '</select>';
												echo '</div>';
												
												echo '<div class="search_col_3">'.$this->translator->translator('settings_search_respective_search_title'). ' &nbsp;&nbsp;';
												echo '<input type="radio" name="text_search_option'.$cnt.'" id="text_search_option'.$cnt.'" value="0"';
												if($entry['text_search_option'] == '0')
												{
												 echo ' checked="checked" ';
												}
												echo '/>'; 
												echo  $this->translator->translator('settings_search_logic_optional');
												echo '&nbsp;&nbsp;<input type="radio" name="text_search_option'.$cnt.'" id="text_search_option'.$cnt.'" value="1"';
												if($entry['text_search_option'] == '1')
												{
												 echo ' checked="checked" ';
												}
												echo '/>'; 
												echo  $this->translator->translator('settings_search_logic_mandatory');
												echo '</div>';
												
												echo '<div class="search_col_4">';
												if($cnt == 0)
												{
													echo '<div class="add-btn ui-widget-content-1"><a href="javascript:void(0);" rel="text" class="add_new_search_btn_link"><span class="icon add">'.$this->translator->translator('settings_search_field_add_new').'</span></a></div>'; 							
												}
												else
												{
													echo '<div class="remove-btn ui-widget-content-1"><a href="javascript:void(0);" rel="text" class="remove_search_btn_link"><span class="icon delete">'.$this->translator->translator("settings_search_field_remove").'</span></a></div>';
												}
												echo '</div></div>';
												
												$cnt++;
											}
										}
										$text_cnt = $cnt;
									}
									else
									{
										echo '<div><div class="search_col_1">';
										echo '<select name="text_search_field0" id="text_search_field0" class="dynamic_search_field"><option value="">'.$this->translator->translator('common_select').'</option>'.$text_search_field;
										echo '</div>';
										
										echo '<div class="search_col_2">';
										echo '<select name="text_search_type0" id="text_search_type0" class="dynamic_search_field">'.$text_search_type;
										echo '</div>';
										
										echo '<div class="search_col_3">'.$this->translator->translator('settings_search_respective_search_title'). ' &nbsp;&nbsp;';
										echo '<input type="radio" name="text_search_option0" id="text_search_option0" value="0" checked="checked" />&nbsp;'; 
										echo  $this->translator->translator('settings_search_logic_optional');
										echo '&nbsp;&nbsp;<input type="radio" name="text_search_option0" id="text_search_option0" value="1" />&nbsp;'; 
										echo  $this->translator->translator('settings_search_logic_mandatory');
										echo '</div>';
										
										echo '<div class="search_col_4">';
										echo '<div class="add-btn ui-widget-content-1"><a href="javascript:void(0);" rel="text" class="add_new_search_btn_link"><span class="icon add">'.$this->translator->translator('settings_search_field_add_new').'</span></a></div>';
										echo '</div></div>';
										$text_cnt = 1;
									}
								?>					
						</div>
                   	</fieldset>
                   	<!-- Text Search End-->
					
                    <!-- Neumaric Search Start -->
                    <fieldset class="fieldset">
                    	<legend class="legend dynamic_search_title"><?php echo $this->translator->translator('settings_search_numeric_search_title'); ?></legend>
						<div class="search_container" id="search_container_numeric">
								<?php 
									if($this->search_settings_data && !empty($this->search_settings_data[0]['numeric_search_field']))
									{
										$cnt = 0;
										foreach($this->search_settings_data as $entry)
										{
											if(!empty($entry['numeric_search_field']))
											{
												echo '<div><div class="search_col_1">';
													echo '<select name="numeric_search_field'.$cnt.'" id="numeric_search_field'.$cnt.'"  class="dynamic_search_field"><option value="">'.$this->translator->translator('common_select').'</option>';
													foreach ($this->table_fields_arr as $key => $value)
													{
														$selected = ($value == $entry['numeric_search_field']) ? ' selected="selected" ' : '';
														echo '<option value="'.$value.'"'.$selected.'>'.$value.'</option>';
													}
													echo '</select>';
												echo '</div>';
												
												echo '<div class="search_col_2">';
													echo '<select name="numeric_search_type'.$cnt.'" id="numeric_search_type'.$cnt.'" class="dynamic_search_field">';
													foreach ($this->numeric_search_logic as $key => $value)
													{
														$selected = ($key == $entry['numeric_search_type']) ? ' selected="selected" ' : '';
														echo '<option value="'.$key.'"'.$selected.'>'.$value.' ('.$key.')</option>';
													}					
													echo '</select>';
												echo '</div>';
												
												echo '<div class="search_col_3">'.$this->translator->translator('settings_search_respective_search_title'). ' &nbsp;&nbsp;';
												echo '<input type="radio" name="numeric_search_option'.$cnt.'" id="numeric_search_option'.$cnt.'" value="0"';
												if($entry['numeric_search_option'] == '0')
												{
												 echo ' checked="checked" ';
												}
												echo '/>'; 
												echo  $this->translator->translator('settings_search_logic_optional');
												echo '&nbsp;&nbsp;<input type="radio" name="numeric_search_option'.$cnt.'" id="numeric_search_option'.$cnt.'" value="1"';
												if($entry['numeric_search_option'] == '1')
												{
												 echo ' checked="checked" ';
												}
												echo '/>'; 
												echo  $this->translator->translator('settings_search_logic_mandatory');
												echo '</div>';
												
												echo '<div class="search_col_4">';
												if($cnt == 0)
												{
													echo '<div class="add-btn ui-widget-content-1"><a href="javascript:void(0);" rel="numeric" class="add_new_search_btn_link"><span class="icon add">'.$this->translator->translator('settings_search_field_add_new').'</span></a></div>';
												}
												else
												{
													echo '<div class="remove-btn ui-widget-content-1"><a href="javascript:void(0);" rel="numeric" class="remove_search_btn_link"><span class="icon delete">'.$this->translator->translator("settings_search_field_remove").'</span></a></div>';
												}
												echo '</div></div>';
												
												$cnt++;
											}
										}
										$numeric_cnt = $cnt;
									}
									else
									{
										echo '<div><div class="search_col_1">';
										echo '<select name="numeric_search_field0" id="numeric_search_field0" class="dynamic_search_field"><option value="">'.$this->translator->translator('common_select').'</option>'.$text_search_field;
										echo '</div>';
										
										echo '<div class="search_col_2">';
										echo '<select name="numeric_search_type0" id="numeric_search_type0" class="dynamic_search_field">'.$numeric_search_type;
										echo '</div>';
										
										echo '<div class="search_col_3">'.$this->translator->translator('settings_search_respective_search_title'). ' &nbsp;&nbsp;';
										echo '<input type="radio" name="numeric_search_option0" id="numeric_search_option0" value="0" checked="checked" />&nbsp;'; 
										echo  $this->translator->translator('settings_search_logic_optional');
										echo '&nbsp;&nbsp;<input type="radio" name="numeric_search_option0" id="numeric_search_option0" value="1" />&nbsp;'; 
										echo  $this->translator->translator('settings_search_logic_mandatory');
										echo '</div>';
										
										echo '<div class="search_col_4">';
										echo '<div class="add-btn ui-widget-content-1"><a href="javascript:void(0);" rel="numeric" class="add_new_search_btn_link"><span class="icon add">'.$this->translator->translator('settings_search_field_add_new').'</span></a></div>';
										echo '</div></div>';
										$numeric_cnt = 1;
									}
								?>
						</div>
                   	</fieldset>
                   	<!-- Numeric Search End -->
					
                    <!-- Date & Search Start -->
                    <fieldset class="fieldset last">
                    	<legend class="legend dynamic_search_title"><?php echo $this->translator->translator('settings_search_date_search_title'); ?></legend>
						<div class="search_container" id="search_container_date">
								<?php 
									if($this->search_settings_data && !empty($this->search_settings_data[0]['date_search_field']))
									{
										$cnt = 0;
										foreach($this->search_settings_data as $entry)
										{
											if(!empty($entry['date_search_field']))
											{
												echo '<div><div class="search_col_1">';
													echo '<select name="date_search_field'.$cnt.'" id="date_search_field'.$cnt.'"  class="dynamic_search_field"><option value="">'.$this->translator->translator('common_select').'</option>';
													foreach ($this->table_fields_arr as $key => $value)
													{
														$selected = ($value == $entry['date_search_field']) ? ' selected="selected" ' : '';
														echo '<option value="'.$value.'"'.$selected.'>'.$value.'</option>';
													}
													echo '</select>';
												echo '</div>';
												
												echo '<div class="search_col_2">';
													echo '<select name="date_search_type'.$cnt.'" id="date_search_type'.$cnt.'" class="dynamic_search_field">';
													foreach ($this->date_search_logic as $key => $value)
													{
														$selected = ($key == $entry['date_search_type']) ? ' selected="selected" ' : '';
														echo '<option value="'.$key.'"'.$selected.'>'.$value.' ('.$key.')</option>';
													}					
													echo '</select>';
												echo '</div>';
												
												echo '<div class="search_col_3">'.$this->translator->translator('settings_search_respective_search_title'). ' &nbsp;&nbsp;';
												echo '<input type="radio" name="date_search_option'.$cnt.'" id="date_search_option'.$cnt.'" value="0"';
												if($entry['date_search_option'] == '0')
												{
												 echo ' checked="checked" ';
												}
												echo '/>'; 
												echo  $this->translator->translator('settings_search_logic_optional');
												echo '<input type="radio" name="date_search_option'.$cnt.'" id="date_search_option'.$cnt.'" value="1"';
												if($entry['date_search_option'] == '1')
												{
												 echo ' checked="checked" ';
												}
												echo '/>'; 
												echo  $this->translator->translator('settings_search_logic_mandatory');
												echo '</div>';
												
												echo '<div class="search_col_4">';
												if($cnt == 0)
												{
													echo '<div class="add-btn ui-widget-content-1"><a href="javascript:void(0);" rel="date" class="add_new_search_btn_link"><span class="icon add">'.$this->translator->translator('settings_search_field_add_new').'</span></a></div>';
												}
												else
												{
													echo '<div class="remove-btn ui-widget-content-1"><a href="javascript:void(0);" rel="date" class="remove_search_btn_link"><span class="icon delete">'.$this->translator->translator("settings_search_field_remove").'</span></a></div>';
												}
												echo '</div></div>';
												
												$cnt++;
											}
										}
										$date_cnt = $cnt;
									}
									else
									{
										echo '<div><div class="search_col_1">';
										echo '<select name="date_search_field0" id="date_search_field0" class="dynamic_search_field"><option value="">'.$this->translator->translator('common_select').'</option>'.$text_search_field;
										echo '</div>';
										
										echo '<div class="search_col_2">';
										echo '<select name="date_search_type0" id="date_search_type0" class="dynamic_search_field">'.$date_search_type;
										echo '</div>';
										
										echo '<div class="search_col_3">'.$this->translator->translator('settings_search_respective_search_title'). ' &nbsp;&nbsp;';
										echo '<input type="radio" name="date_search_option0" id="date_search_option0" value="0" checked="checked" />&nbsp;'; 
										echo  $this->translator->translator('settings_search_logic_optional');
										echo '&nbsp;&nbsp;<input type="radio" name="date_search_option0" id="date_search_option0" value="1" />&nbsp;'; 
										echo  $this->translator->translator('settings_search_logic_mandatory');
										echo '</div>';
										
										echo '<div class="search_col_4">';
										echo '<div class="add-btn ui-widget-content-1"><a href="javascript:void(0);" rel="date" class="add_new_search_btn_link"><span class="icon add">'.$this->translator->translator('settings_search_field_add_new').'</a></div>';
										echo '</div></div>';
										$date_cnt = 1;
									}
								?>
						</div>
                    </fieldset>
                   	<!-- Date & Search End-->
				</div>
					
                <div><input type="hidden" name="text_cnt" id="text_cnt" value="<?php echo $text_cnt; ?>" /> <input type="hidden" name="numeric_cnt" id="numeric_cnt" value="<?php echo $numeric_cnt; ?>" /> <input type="hidden" name="date_cnt" id="date_cnt" value="<?php echo $date_cnt; ?>" /></div>            	
			</form>
        </div>
        
		<div id="floatingbar">
        	<h1><?php echo $this->translator->translator('settings_search_page_name'); ?></h1>
            
            <div class="button-container">
                <span class="button  ui-widget-content-1"><a href="<?php echo $this->url( array('module' => $this->back_module_name,'controller' => $this->back_controller_name, 'action'     => $this->back_action_name ), 'adminrout',    true) ?>"><span class="icon back"><?php echo $this->translator->translator('common_back'); ?></span></a></span>
                <span class="button  ui-widget-content-1"><a href="javascript:  void(0);" class="reset_btn"><span class="icon reset"><?php echo $this->translator->translator('common_reset'); ?></span></a></span>
                <span class="button  ui-widget-content-1"><a href="javascript: void(0);" class="save_search_btn"><span class="icon save"><?php echo $this->translator->translator('common_save'); ?></span></a></span>
            </div>
		</div>
		<?php endif; ?>	
		<div id="dialog_msg" title="<?php echo $this->translator->translator('common_dialog_msg',$this->translator->translator('settings_page_name')); ?>"></div>
		<div id="dialog_loader" style="display:none" title="<?php echo $this->translator->translator('common_processing_language'); ?>"><div id="progressbar" style="height:22px;margin-top:30px;"></div></div>
</div>